package com.twang.stock.task.old;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.enmu.StockStatusEnum;
import com.twang.stock.entity.CompanyCodeInfo;
import com.twang.stock.entity.StockInfo;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import com.twang.stock.service.impl.MinutesVolRuleService;
import com.twang.stock.service.jinyan.HolidayService;
import com.twang.stock.service.jinyan.StockInfoService;
import com.twang.stock.threadPool.AsycTaskExecutorConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 *分析买点 计算强度：每日14点56分更新
 */
@Slf4j
@Service
public class AnalyseBuyAndsellTask extends  BaseTask{

    @Autowired
    private MinutesVolRuleService minutesVolRuleService;
    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;

    @Autowired
    private AsycTaskExecutorConfig asycTaskExecutorConfig;

    @Autowired
    private  HolidayService holidayService;

    @Autowired
    private StockInfoService stockInfoService;


   // @Scheduled(cron = "0 30 15 * * ?")
    public void execute() {
        if (!isWork()) {
            return;
        }
        List<CompanyCodeInfo> stockList = companyCodeInfoMapper.selectAll();
        for (CompanyCodeInfo companyCodeInfo : stockList) {
            asycTaskExecutorConfig.taskQueueExecutor().execute(() -> excuteRunable(companyCodeInfo));
        }
    }

    private void excuteRunable(CompanyCodeInfo companyCodeInfo) {
        //上一交易日
       StockInfo stockInfoToday = minutesVolRuleService.getStockInfoByDateDESC(companyCodeInfo.getCompanyCode());
        //StockInfo stockInfoToday =  stockInfoService.getCodeToday(companyCodeInfo.getCompanyCode(),holidayService.getDayByNum(1));
        if (null != stockInfoToday) {
            //当日交易日
            StockInfo stockInfoYesterDay = minutesVolRuleService.getCode(companyCodeInfo.getCompanyCode(), stockInfoToday.getDate());
            if (null == stockInfoYesterDay || null == stockInfoToday.getVolMay1() || null == stockInfoToday.getPriceMay5()) {
                log.error(" stockInfoToday 今日无数据 shortName={} date={}", companyCodeInfo.getShortName(), stockInfoToday.getDate());
                return;
            }
            companyCodeInfo.setBuyOrSell(null);
            companyCodeInfo.setStrongRiseVol(null);
            companyCodeInfo.setStrongRise(null);
            try {
                //触发规则-旁段买卖点
                //stockInfoToday.getPrice().compareTo(stockInfoToday.getPriceMay5()) > 1
                //
                if (stockInfoToday.getVolMay1() > stockInfoToday.getVolMay5() && stockInfoToday.getZhangdie().compareTo(BigDecimal.ZERO) == 1) {
                    if (null != companyCodeInfo) {
                        companyCodeInfo.setBuyOrSell(StockStatusEnum.BUY.getMessage());
                        companyCodeInfoMapper.updateByPrimaryKey(companyCodeInfo);
                        //TODO 关联关注表，邮件预警
                    }
                } else {
                    companyCodeInfo.setBuyOrSell(StockStatusEnum.SELL.getMessage());
                    companyCodeInfoMapper.updateByPrimaryKey(companyCodeInfo);
                    //TODO 关联关注表，邮件预警
                }
                //计算价格强度
                compareprice(0, companyCodeInfo, stockInfoToday.getDate());
                //
                compareVol(0, companyCodeInfo, stockInfoToday.getDate());
                log.info(" 更新 shortName={} date={} 信号={} 强度={}", companyCodeInfo.getShortName(), stockInfoToday.getDate(), companyCodeInfo.getBuyOrSell(), companyCodeInfo.getStrongRise());
                companyCodeInfoMapper.updateByPrimaryKey(companyCodeInfo);
            } catch (Exception e) {
                log.error(" stockInfoToday={}, \n stockInfoYesterDay={} \n error={}", JSONObject.toJSONString(stockInfoToday), JSONObject.toJSONString(stockInfoYesterDay), e);
            }
        }
    }

    /**
     * 计算强度
     * @param i
     * @param companyCodeInfo
     * @param toDay
     */
    private void compareprice(Integer i,CompanyCodeInfo companyCodeInfo,String toDay) {
        StockInfo stockInfoToday = minutesVolRuleService.getCodeToday(companyCodeInfo.getCompanyCode(), toDay);
        if (null != stockInfoToday && null != stockInfoToday.getPrice() && null != stockInfoToday.getPriceMay5()) {
            if ((stockInfoToday.getPrice().compareTo(stockInfoToday.getPriceMay5())) == 1) {
                ++i;
                companyCodeInfo.setStrongRise(i);
                StockInfo stockInfoYesterDay = minutesVolRuleService.getCode(companyCodeInfo.getCompanyCode(), stockInfoToday.getDate());
                if (null != stockInfoYesterDay) {
                    compareprice(i, companyCodeInfo, stockInfoYesterDay.getDate());
                }
            }
        }else{
            companyCodeInfo.setStrongRise(0);
        }
    }

    /**
     * 计算强度
     * @param i
     * @param companyCodeInfo
     * @param toDay
     */
    private void compareVol(Integer i,CompanyCodeInfo companyCodeInfo,String toDay) {
        StockInfo stockInfoToday = minutesVolRuleService.getCodeToday(companyCodeInfo.getCompanyCode(), toDay);
        if (null != stockInfoToday && null != stockInfoToday.getVolMay1() && null != stockInfoToday.getVolMay5()) {
            if ((stockInfoToday.getVolMay1().compareTo(stockInfoToday.getVolMay5())) == 1) {
                ++i;
                companyCodeInfo.setStrongRiseVol(i);
                StockInfo stockInfoYesterDay = minutesVolRuleService.getCode(companyCodeInfo.getCompanyCode(), stockInfoToday.getDate());
                if (null != stockInfoYesterDay) {
                    compareVol(i, companyCodeInfo, stockInfoYesterDay.getDate());
                }
            }
        }else{
            companyCodeInfo.setStrongRiseVol(0);
        }
    }
}
